package com.luck.easyfoodbatch.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.EvaluationAdapter2;
import com.luck.easyfoodbatch.mvp.model.EvaluateM;
import com.luck.easyfoodbatch.mvp.order.EvaluateListModel;
import com.luck.easyfoodbatch.mvp.order.EvaluateListPresenter;
import com.luck.easyfoodbatch.mvp.order.EvaluateListView;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyrecyclerview.EasyRecyclerView;
import com.luck.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.fragment
 * email：893855882@qq.com
 * data：16/9/30
 */
public class EvaluationFragment2 extends BaseFragment implements EvaluateListView, RecyclerArrayAdapter.OnLoadMoreListener {
    @BindView(R.id.recyclerView)
    EasyRecyclerView recyclerView;
    private List<EvaluateM.DataBean> list = new ArrayList<>();
    private EvaluationAdapter2 adapter;
    private String gid = "";
    private EvaluateListModel listModel;
    private int totalPage = 1;
    private int page = 1;
    private boolean isRefresh = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fc_evaluation2, container, false);
            ButterKnife.bind(this, rootView);
            initView();
        }

        return rootView;
    }

    private void initView() {
        gid = getActivity().getIntent().getStringExtra("gid");
        listModel = new EvaluateListPresenter(this, mContext);
        listModel.requestEvaluateList(page+"",gid);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0.5F), getResources().getColor(R.color.line_color)));
        recyclerView.setLayoutManager(manager);
        adapter = new EvaluationAdapter2(mContext);
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore);
        adapter.addAll(list);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void evaluateListSuccess(EvaluateM model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                if (list == null) {
                    list = new ArrayList<>();
                }
                list.addAll(model.getData());
                adapter.clear();
                adapter.addAll(list);
                adapter.notifyDataSetChanged();
            } else {
                T.showToast(mContext, model.getMsg());
            }
        }
    }

    @Override
    public void httpStatusError() {

    }
    @Override
    public void hideLoading() {
        //hideShowProgress();
    }
    @Override
    public void onLoadMore() {
        //刷新
        isRefresh = false;
        page++;
        if (!isNetworkConnected()) {
            adapter.pauseMore();
            return;
        }
        if (page > totalPage) {
            adapter.stopMore();
            return;
        }
        listModel.requestEvaluateList(page+"",gid);
    }
}
